package com.fysports.web.model.system;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.fysports.web.kit.tree.ITreeRecursion;
import com.fysports.web.model.BaseModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 系统权限表
 * Created by konbluesky
 * Date : 15/7/29 下午6:23
 * Project :jdmdata_server
 */
public class SystemRole extends BaseModel<SystemRole> implements ITreeRecursion<SystemRole> {
    private static Log log = Log.getLog(SystemRole.class);
    public final static SystemRole dao = new SystemRole();
    public static final String COL_PARENTUUID = "puuid";
    public static final String COL_ROLENAME = "rolename";


    public SystemRole() {
        if (this.getStr(COL_UUID) == null || this.getStr(COL_UUID).length() == 0) {
            this.set(COL_UUID, generateUUID());
        }
    }


    public SystemRole(SystemRole e) {
        this._setAttrs(e);
    }


    /**
     * getNTChilds
     *
     * @param nodeid
     * @return
     */
    public List<SystemRole> getParents(String nodeid) {
        String sql = "select * from " + getTable().getName()
                + " where liveflag=1 and FIND_IN_SET(uuid,getRoleParents('0',?))";
        List<SystemRole> list = SystemRole.dao.find(sql, nodeid);
        return list;
    }


    /**
     * getNTParents
     *
     * @param nodeid
     * @return
     */
    public List<SystemRole> getChilds(String nodeid) {
        String sql = "select * from " + getTable().getName()
                + " where liveflag=1 and FIND_IN_SET(uuid,getRoleChilds(?))";
        List<SystemRole> list = SystemRole.dao.find(sql, nodeid);
        return list;
    }


    /**
     * 添加对应角色的菜单
     *
     * @param ids 菜单id数组
     */
    public boolean addMenuList(String... ids) {
        if (ids == null || ids.length == 0)
            throw new IllegalArgumentException("ids为无效的菜单列表");
        String roleuuid = getStr(COL_UUID);
        if (roleuuid == null) {
            log.error("roleuuid不能为空");
            return false;
        }
        String resctype = "menu";
        String sql = "insert into sys_role_resource(uuid,roleuuid,rescuuid,resctype,createtime) "
                + "values(?,?,?,?,?)";
        Object[][] params = new Object[ids.length][5];
        for (int i = 0; i < ids.length; i++) {
            params[i][0] = generateUUID();
            params[i][1] = roleuuid;
            params[i][2] = ids[i];
            params[i][3] = resctype;
            params[i][4] = new Date();
        }
        Db.update("delete from sys_role_resource where roleuuid=?", roleuuid);
        int[] result = Db.batch(sql, params, 1000);
        return result != null && result.length == ids.length;
    }


    public List<SystemMenu> getRoleMenu() {
        String roleuuid = getStr(COL_UUID);
        if (roleuuid == null) {
            log.error("roleuuid不能为空");
            return new ArrayList<SystemMenu>(0);
        }
        String sql="select * from sys_menu ";
        //非管理员就按照权限分派来取
        if(!isAdmin()){
            sql += " where code in (" +
                    " select rescuuid from sys_role_resource where  roleuuid=? and resctype='menu'" +
                    " ) ";
        }
        sql+=" order by orders";
        if(!isAdmin()) {
            return SystemMenu.dao.find(sql, roleuuid);
        }else{
            return SystemMenu.dao.find(sql);
        }
    }

    public boolean isAdmin(){
        return this.getBoolean("isadmin");
    }

    @Override public String getParentCode() {
        return this.getStr("puuid");
    }


    @Override public String getCode() {
        return this.getStr("uuid");
    }


    @Override public String getName() {
        return this.getStr("rolename");
    }


    @Override public ITreeRecursion getInstance() {
        return new SystemRole(this);
    }
}
